use crate::config1::REDIS_CODE_SOURCE_DURATION;
use crate::errors::MyError;
use redis::{Commands, Connection};
use std::result::Result;

const REDIS_PREFIX_SOURCE: &str = "img_code_source";

pub fn set_data_str(
    redis_conn: &mut Connection,
    data_prefix: &str,
    key: &str,
    data: &str,
) -> Result<(), MyError> {
    Ok(redis_conn.set(format!("{}:{}", data_prefix, key), data)?)
}

pub fn set_code_source(redis_conn: &mut Connection, key: &str, data: &str) -> Result<(), MyError> {
    Ok(redis_conn.set_ex(
        format!("{}:{}", REDIS_PREFIX_SOURCE, key),
        data,
        *REDIS_CODE_SOURCE_DURATION,
    )?)
}

pub fn exists_code_source(redis_conn: &mut Connection, key: &str) -> Result<bool, MyError> {
    let result: bool = redis_conn.exists(format!("{}:{}", REDIS_PREFIX_SOURCE, key))?;
    Ok(result)
}

pub fn exists_data(
    redis_conn: &mut Connection,
    data_prefix: &str,
    key: &str,
) -> Result<bool, MyError> {
    let result: bool = redis_conn.exists(format!("{}:{}", data_prefix, key))?;
    Ok(result)
}
